 function ghvec=plot_TwoIrfs(irf_mat1,irf_mat2,namesRows,namesPages,namesModels,nameGraph,fonts) 
% =========================================================================
% PLOT_IRF.M 
%  
% ghvec=plot_irf(irf_mat,stpos,shockpos,legends,stnames,shonames,addtotit) 
%
% Given a matrix of IRFS  IRFMAT of dimension [NIRF NPER+1 NX ] 
% NIRF    Number of States 
% NPER    Number of periods 
% NX      Number of pages 
% Note: The dimensions of a 3-dimensional object are referred to as
% row,column and pages. 
% Here the pages correspond to SHOCKS 
% perturbations, plot the IRF to the states indicated in STPOS and to the
% shocks given in SHOCKPOS. Creating one figure for each SHOCK 
% 
% If STPOS (SHOCKPOS) not provided, whole matrix of states (shocks) will be produced 
% 
% Additional inputs 
% ------------------
% LEGENDS       Cell indicating which each line stands for 
% STNAMES       Cell with names of full length state vector (NSTx1) 
% SHONAMES      Cell with names of shocks (NX x 1 ) 
% ADDTOTIT      String to add to the SUPTITLE e.g. STORAGE 
% 
% Output
% GHVEC         Vector with the handles to each figure
%
% Alejandro Justiniano  January 30 2008 
% =========================================================================

% 1. Check dimensions for matrix and assign names to states, shocks and
% legends if not provided 
% =========================================================================
[nst1 nper1 nx1]=size(irf_mat1);nper=nper1-1; 
[nst2 nper2 nx2]=size(irf_mat2);

if nst1~=nst2;error('Rows do not match');end 
if nper1~=nper2;error('Columns do not match');end 
if nx1  ~=nx2   ;error('Pages do not match');end  

if length(namesRows)~=nst1;  error('namesRows  does not match number of rows'); end 
if length(namesPages)~=nx1; error('namesPages does not match number of pages'); end 

if nargin ==8 && ~isempty(axfont);
    set(0,'DefaultAxesFontSize',axfont);
end


% 2. Define General Settings for the PLOTS 
% =========================================================================
style={'-','--',':','-','-*','--.',':*'}; 
width=[ 2.5    2   3  2    2   3     2  ]; 
colormatrix=[0.5    0.5     0.5
             0.0    0.0     0.0]; 
xvec=(0:nper)';

Nrow=ceil(nst1/2); 
Ncol=2; 
if nargin ==7 && ~isempty(fonts);
    set(0,'DefaultAxesFontSize',fonts.axes);
    titfont=fonts.titfont;
else
    if Nrow < 5
        titfont=12; %Font for titles
    elseif Nrow < 8
        titfont=10;
    else
        titfont=9;
    end
end 
% 3. One figure per shock, subfigure per state 
% =========================================================================
tolSmall=1e-6; 
ghvec=zeros(nst1,1);
for ii=1:nx1;
    
    ghvec(ii)=figure;
    
    for jj=1:nst1

        subplot(Nrow,Ncol,jj);
        
        % Makes small numbers zero 
        Y1=squeeze(irf_mat1(jj,:,ii));
        Y2=squeeze(irf_mat2(jj,:,ii));
        flatten1=find(abs(Y1) < tolSmall);
        if ~isempty(flatten1)
            Y1(flatten1)=zeros(length(flatten1),1);
        end
        flatten2=find(abs(Y2) < tolSmall);
        if ~isempty(flatten2)
            Y2(flatten2)=zeros(length(flatten2),1);
        end
                
        hand=plot(xvec,Y1,style{1},'LineWidth',width(1));
        set(hand,'Color',colormatrix(1,:));
        hold on; 
        hand=plot(xvec,Y2,style{2},'LineWidth',width(2));
        set(hand,'Color',colormatrix(2,:));
        
        edgeoff=0.01; 
        yLimvec=[min( [Y1(:);Y2(:);0] )-edgeoff...
              max( [Y1(:);Y2(:);0] )+edgeoff]; 
        
        % Title and Legend
        title(namesRows{jj},'FontSize',titfont);
        set(gca,'Box','off')
        xlim([0 nper]); 
        ylim([yLimvec(1) yLimvec(2)]);  
    end    
    handleg=legend([220 10 0.4 0.4],namesModels,'orientation','horizontal','FontSize',11); 
    set(handleg,'Box','Off'); 
    
    set(ghvec(ii),'PaperPosition',[-0.25 0.25 8 10.5])
    hand=suptitle([namesPages{ii},' IRF ',nameGraph]);
    set(hand,'FontSize',titfont+2); 
    
end
set(0,'DefaultAxesFontSize','remove');